#!/usr/local/bin/bash

function outOfDate () {
    if [ ! -f $2 ]; then
        return 0
    fi
    if [ "`ls -1t $1 $2 | head -1`" == "$1" ]; then
        return 0
    fi

    return 1
}

export CLASSPATH=../
export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin

jar_path="classes/"
for f in `find lib/ -iname "*.jar"`; do
	jar_path=$jar_path:$f
done

dorestart="0"
for f in `find classes/ -iname "*.java"`; do
    class=`dirname $f`/`basename -s .java $f`.class
    
    if outOfDate $f $class; then
        dorestart="1"
        echo "Compiling" $f >> compile-log.txt
        /usr/local/bin/javac -d classes -cp $jar_path $f >> compile-log.txt 2>> compile-err.txt
    fi
done

if [ "$dorestart" = "1" ]; then 
    /usr/local/bin/sudo /usr/local/etc/rc.d/tomcat6 restart
fi

